package net.huashitong.Project.web;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.BuildProjects;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.BuildProjectsFilter;
import net.huashitong.Project.service.BuildProjectsService;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.user.entity.BnpUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.huashitong.utils.UtilsTools.getCurrentMonth;


@Controller
@RequestMapping(value = "/web/buildProject")
public class BuildProjectController {
    Logger logger = LoggerFactory.getLogger("BuildProjectController");
    @Autowired
    private BuildProjectsService buildProjectsService;
    @Autowired
    private NewProjectsService newProjectsService;


    /**
     * 查询在建项目进度
     * @param projectId
     * @param model
     * @return
     */
    @RequestMapping(value = "/queryList")
    public String queryList(@RequestParam String projectId, Model model){
        model.addAttribute("leftType", "我的项目");
        BuildProjectsFilter filter = new BuildProjectsFilter();
        NewProjects newProject = newProjectsService.getById(projectId);
        filter.setProjectId(projectId);
        List<BuildProjects> list = buildProjectsService.queryBuildProjectsByFilter(filter);
        //当前月
        String currentMonth = getCurrentMonth();
        model.addAttribute("currentMonth",currentMonth.substring(0,4)+"-"+currentMonth.substring(4,6));
        model.addAttribute("list", list);
        model.addAttribute("newProject", newProject);
        model.addAttribute("projectId", projectId);
        return "/modules/buildProjects/buildProjectRecords";
    }

    /**
     * 添加或者修改在建项目进度
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addBuildRecord", method = RequestMethod.POST)
    public Map<String, Object> addRelocatePlan(@RequestBody BuildProjectsFilter filter, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        Map<String, Object> result = new HashMap<>();
        if(ObjectUtils.isBlank(filter.getYearTarget())||
                ObjectUtils.isBlank(filter.getYearPlanInv())||
                ObjectUtils.isBlank(filter.getThisTimeFinishInv())||
                ObjectUtils.isBlank(filter.getThisMonthInPlaceInv())||
                ObjectUtils.isBlank(filter.getThisYearCumulativeInv())||
                ObjectUtils.isBlank(filter.getThisYearCumulativeInPlaceInv())||
                ObjectUtils.isBlank(filter.getCumulativeInv())||
                ObjectUtils.isBlank(filter.getCumulativeInPlaceInv())||
                ObjectUtils.isBlank(filter.getStartState())||
                ObjectUtils.isBlank(filter.getCompletedState())||
                ObjectUtils.isBlank(filter.getPlanStartDate())||
                ObjectUtils.isBlank(filter.getPlanCompletedDate())||
                ObjectUtils.isBlank(filter.getFactStartDate())||
                ObjectUtils.isBlank(filter.getFactCompletedDate())||
                ObjectUtils.isBlank(filter.getProgress())||
                ObjectUtils.isBlank(filter.getProblem())||
                ObjectUtils.isBlank(filter.getDataTime())){
            result.put("code", "201");
            result.put("message", "提交失败,请按照要求填写。");
            return result;
        }
        buildProjectsService.addBuildRecord(filter, bnpUser);
        result.put("code", "200");
        result.put("message", "成功添加或者修改进度报告");
        return result;
    }
}
